import 'package:flutter/material.dart';
import 'package:osom_rating/osom_rating.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio = new Dio();

class Baobiao extends StatefulWidget {
  Baobiao({Key key}) : super(key: key);

  _BaobiaosState createState() => _BaobiaosState();
}

class _BaobiaosState extends State<Baobiao> {
  double _star = 0.0;
  var user_id ;
  var xuehao ;
  var classban ;
  var xueyuanban ;
  var jifen ;
  var _quanban;
  var _quanyuan;
  var _quanxiao;
  var _quanbanindex;
  var _quanyuanindex;
  var _quanxiaoindex;
  String duowei;
  getName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      user_id = sharedPreferences.getString('user_id');
      xuehao = sharedPreferences.getString('number');
      getpeople();
      setState(() {});
  }

  Future getdate() async {
   Response response = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/student?query={"class":"${classban}"}&sort=-point');
    _quanban = response.data;
    Response response2 = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/student?query={"college":"${xueyuanban}"}&sort=-point');
    _quanyuan = response2.data;
    Response response3 = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/student?sort=-point');
    _quanxiao = response3.data;

    quanban();
    quanyuan();
    quanxiao();
  }

  void quanban(){
    for(var i = 0;i<_quanban.length;i++){
      if(_quanban[i]['_id'] == user_id){
        _quanbanindex = _quanban.indexOf(_quanban[i])+1;
        break;
      }
    }
    setState(() {});
  }
  void quanyuan(){
    for(var i = 0;i<_quanyuan.length;i++){
      if(_quanyuan[i]['_id'] == user_id){
        _quanyuanindex = _quanyuan.indexOf(_quanyuan[i])+1;
        break;
      }
    }
    setState(() {});
  }
  void quanxiao(){
    for(var i = 0;i<_quanxiao.length;i++){
      if(_quanxiao[i]['_id'] == user_id){
        _quanxiaoindex = _quanxiao.indexOf(_quanxiao[i])+1;
        break;
      }
    }
    setState(() {});
  }

 //通过ID获取人
 Future getpeople() async {
    if(xuehao.length>=10){
     Response responsestudent = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');
      classban = responsestudent.data['class'];
      xueyuanban = responsestudent.data['college'];
      jifen = responsestudent.data['point'];
      if(jifen<=30){_star = 2.0;duowei="勤奋蜜蜂";}else if(30<jifen&&jifen<=70){_star = 4.0;duowei="优秀学生";}else if(70<jifen&&jifen<=120){_star = 6.0;duowei="三好学生";}else if(120<jifen&&jifen<=180){_star = 8.0;duowei="四好少年";}else{_star = 10.0;duowei="翰林学士";}
      getdate();
    }
    else{
      Response responselaoshi = await dio.get(
        'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}');
        jifen = responselaoshi.data['point'];
        _quanbanindex = 0;
        _quanyuanindex = 0;
        _quanxiaoindex = 0;
        setState(() {});
    }
 }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getName();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('学习报表'),
          centerTitle: true,
        ),
        body: Stack(
          children: <Widget>[
            Container(
              width: 400,
              child: Image.asset(
                'images/bj.jpg',
                fit: BoxFit.cover,
              ),
            ),
            Container(
              width: 320,
              height: 110,
              margin: EdgeInsets.fromLTRB(20, 20, 0, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(
                    width: 1, color: Colors.white),
                borderRadius: BorderRadius.all(Radius.circular(6.0)),
              ),
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 15,
                            ),
                            Text('成长总积分',style: TextStyle(fontWeight: FontWeight.bold),),
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(
                          '${jifen}',
                          textAlign: TextAlign.right,
                          style: TextStyle(
                            color: Color.fromRGBO(227, 60, 100, 1),
                            fontWeight: FontWeight.bold,
                            fontSize: 22,
                          ),
                        ),
                      ),
                      SizedBox(
                        width: 15,
                      ),
                    ],
                  ), //分
                  SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 15,
                            ),
                            Text('你的段位',style: TextStyle(fontSize: 12,color: Color.fromRGBO(167, 167, 167, 1)),),
                          ],
                        ),
                      ),
                      SizedBox(width: 25,),
                      Expanded(
                        flex: 3,
                        child: Row(
                          children: <Widget>[
                            Text('$duowei',style: TextStyle(color:  Color.fromRGBO(225, 141, 26, 1),fontSize: 12)),
                            SizedBox(width: 7,),
                            OsomRating(
                              rating: this._star,
                              totalStars: 10,
                              colorStar: Color.fromRGBO(225, 141, 26, 1),
                              colorEmptyStar: Color.fromRGBO(220, 220, 200, 1),
                              sizeStar: 15,
                            )
                          ],
                        )
                      ),
                      SizedBox(
                        width: 15,
                      ),
                    ],
                  ), //分
                  SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 15,
                            ),
                            Text('可成长积分',style: TextStyle(fontSize: 12,color: Color.fromRGBO(167, 167, 167, 1)),),
                          ],
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: Text(
                          '敬请期待',
                          textAlign: TextAlign.right,
                          style: TextStyle(
                              color: Color.fromRGBO(216, 216, 216, 1)),
                        ),
                      ),
                      SizedBox(
                        width: 15,
                      ),
                    ],
                  ), //分
                ],
              ),
            ),
            Container(
              width: 320,
              height: 50,
              margin: EdgeInsets.fromLTRB(20, 140, 0, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(
                    width: 1, color: Colors.white),
                borderRadius: BorderRadius.all(Radius.circular(6.0)),
              ),
              child: Row(
                children: <Widget>[
                  SizedBox(
                    height: 15,
                  ),
                  Expanded(
                    flex: 1,
                    child: Row(
                      children: <Widget>[
                        SizedBox(
                          width: 15,
                        ),
                        Text('你在全校的总排名',style: TextStyle(fontWeight: FontWeight.bold),),
                      ],
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text(
                      '${_quanxiaoindex}',
                      textAlign: TextAlign.right,
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  SizedBox(
                    width: 15,
                  ),
                ],
              ),
            ),
            Container(
              width: 320,
              height: 50,
              margin: EdgeInsets.fromLTRB(20, 200, 0, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(
                    width: 1, color: Colors.white),
                borderRadius: BorderRadius.all(Radius.circular(6.0)),
              ),
              child: Row(
                children: <Widget>[
                  SizedBox(
                    height: 15,
                  ),
                  Expanded(
                    flex: 1,
                    child: Row(
                      children: <Widget>[
                        SizedBox(
                          width: 15,
                        ),
                        Text('你在全院的总排名',style: TextStyle(fontWeight: FontWeight.bold),),
                      ],
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text(
                      '${_quanyuanindex}',
                      textAlign: TextAlign.right,
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  SizedBox(
                    width: 15,
                  ),
                ],
              ),
            ),
            Container(
              width: 320,
              height: 50,
              margin: EdgeInsets.fromLTRB(20, 260, 0, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(
                    width: 1, color: Colors.white),
                borderRadius: BorderRadius.all(Radius.circular(6.0)),
              ),
              child: Row(
                children: <Widget>[
                  SizedBox(
                    height: 15,
                  ),
                  Expanded(
                    flex: 1,
                    child: Row(
                      children: <Widget>[
                        SizedBox(
                          width: 15,
                        ),
                        Text('你在全班的总排名',style: TextStyle(fontWeight: FontWeight.bold),),
                      ],
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text(
                      '${_quanbanindex}',
                      textAlign: TextAlign.right,
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  SizedBox(
                    width: 15,
                  ),
                ],
              ),
            ),
            Container(
              width: 320,
              height: 150,
              margin: EdgeInsets.fromLTRB(20, 320, 0, 0),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border.all(
                    width: 1, color: Colors.white),
                borderRadius: BorderRadius.all(Radius.circular(6.0)),
              ),
              child: Column(
                children: <Widget>[
                  SizedBox(height: 6),
                  Row(
                    children: <Widget>[
                      Container(
                        width: 5,
                        height: 12,
                        margin: EdgeInsets.fromLTRB(8, 0, 8, 0),
                        color: Colors.red,
                      ),
                      Text(
                        '本组学习任务',
                        style: TextStyle(
                            fontWeight: FontWeight.bold, fontSize: 12),
                      )
                    ],
                  ),
                ],
              ),
            )
          ],
        ));
  }
}
